<?
    function Date2DBDate($date, $jh = false) {
       if (!$jh) {$jh = substr(date("Y"),0,2);}
       if (preg_match( '/^[0-9]{1,2}\.[0-9]{1,2}\.([0-9]{2,4})$/i', $date) ) {
         $help = explode(".", $date);
         if (strlen($help[0]) == 1)
           $help[0] = '0' . $help[0];
         if ($help[0] < 32){
           if (strlen($help[1]) == 1)
             $help[1] = '0' . $help[1];
           if ($help[1] < 13){
             if (strlen($help[2]) == 2) {
              $help[2] = $jh . $help[2];
             }
             return $help[2] . '-' . $help[1] . '-' . $help[0];
           }
         }
       }
       return false;
    }

    function DBDate2Date($date,$short=false) {
      if (isDate($date)) {
        if ($short) $date = substr($date,2);
        return implode(".",array_reverse(explode("-",$date)));
      }
      return false;
    }
    function isDate($date) {
      return ($date != '0000-00-00' && $date);
    }

    function now($german=false) {
       if ($german) return date("d.m.Y");
       return date("Y-m-d");
    }

   # Calculate the age given a birth date
    function getAge($birthday) {
      # Falls Datum < 1.1.1970
      if (strtotime($birthday)<0) {
        return floor((date("Ymd")-(int)str_replace('-','',$birthday))/10000);
      } else {
        return date("Y",time()-strtotime($birthday))-1970;
      }
    }
	
    function getTime($time,$text=false) {
      return ($time != '00:00:00') ? substr($time,0,5).(($text) ? ' Uhr':''):'';
    }
	
    function getEmail($mail,$subject='UCCC mail') {
      //return ($mail) ? '<a href="mailto:'.$mail.'?subject='.$subject.'">'.$mail.'</a>':'-';
	  return ($mail)? safeAddress($mail,$mail,$mail,1,0,$subject):'-';
    }
	

	/** Spam fighting mailto links. Stops crawlers getting the email addys and sending heaps of spam...
	// Version 1 - Will change to have subject then use.
	// set $isItSafe = 1 to get escaped HTML, 0 for normal HTML
	// set $xhtml = 1 if you want your page to be valid for XHTML 1.x
	// you can call it like this: (in php)
	// echo safeAddress($entity, $linkText, $titleText, 1, 0); **/
	function safeAddress($emailAddress, $theText, $theTitle, $xhtml, $isItSafe, $subject) 
	{
		$ent = "";
		$userName = "";
		$domainName = "";
		
		for ($i = 0; $i < strlen($emailAddress); $i++) {
			$c = substr($emailAddress, $i, 1);
			if ($c == "@") {
				$userName = $ent;
				$ent = "";
				} else {
				$ent .= "&#" . ord($c) . ";";
				}
		}
	
		$domainName = $ent;
		
		if ($xhtml == 1) {
	
		$endResult = "<script type=\"text/javascript\">
	<!--
	document.write('<a href=\"mailto:$userName&#64;$domainName?subject=$subject\" title=\"$theTitle\">$theText<\/a>');
	// -->
	</script>";
	
		} else {
			$endResult = "<script language=\"JavaScript\" type=\"text/javascript\">
	<!--
	document.write('<a href=\"mailto:$userName&#64;$domainName?subject=$subject\" title=\"$theTitle\">$theText<\/a>');
	// -->
	</script>";
	
		}
		if ($isItSafe) {
			return(htmlentities($endResult));
		} else {
			return($endResult);
		}
	}
	
	
    function getLink($url,$name=false,$target='_blank') {
      return '<a href="'.$url.'" target="'.$target.'" title="'.($name?$name:$url).'">'.($name?$name:((strlen($url)>40)?'Link':$url)).'</a>';
    }
    function getFile($file,$target='_blank') {
      return '<a href="'.DOWNLOADDIR.$file.'" target="'.$target.'" title="'.$file.'">'.$file.'</a>';
    }
    # Converts a reasonable price to
    function FloatToCurrency($preis,$euro=true) {
      if (!$preis = round($preis,2)) return false;
      if (($point = strpos($preis,'.')) !== false) {
        if (strlen( substr($preis,$point+1)) == 1) $preis .= '0';
      } else {
        $preis .= '.00';
      }
      return str_replace('.',',',$preis).(($euro) ? ' &euro;':'');
    }
?>